package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.entity.Role;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.entity.YddTag;
import com.itheima.ydd.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.HTML;
import java.util.List;

@RestController
@RequestMapping("/ydd_tag")
@Slf4j
public class YddTagController {
    @Autowired
    private YddTagService yddTagService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
/*    @Autowired
    private RoleMenuService roleMenuService;*/

    /**
     * 添加
     * @param yddTag
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddTag yddTag){
        log.info("yddTag:{}",yddTag);
       /* //权限管理
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        //获取用户登陆的id  转化为string类型  --前端页码传来的值带有【】 ""  所以要去除点
        String userId = UserHolder.getUserId();
        User userl = userService.getById(userId);
        //分割
        String strip = StringUtils.strip(userl.getRole_id(), "[]");
        String[] split = strip.split(",");

        for (int i = 0; 0 < split.length; i++) {
            //代替去除*/

      //  }
        yddTagService.save(yddTag);
        return ResultData.ok("添加成功",yddTag);
    }

    /**
     * id回显
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData echo(Long id){
        log.info("回显数据",id);
        YddTag tag = yddTagService.getById(id);
        return ResultData.ok("",tag);
    }

    /**
     * 修改数据
     * @param yddTag
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddTag yddTag){
        log.info("修改:{}成功",yddTag);//权限管理
        yddTagService.updateById(yddTag);
        return ResultData.ok("修改成功",yddTag);
    }

    /**
     * 查询数据
     * @param tagname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String tagname ,long page ,long limit){
        //分页构造器
        Page listPage = new Page<>(page,limit);
        LambdaQueryWrapper<YddTag> queryWrapper = new LambdaQueryWrapper<>();
        //判断是否为空
        queryWrapper.like(tagname!=null,YddTag::getTagname,tagname);
        /*if (!("".equals(tagname))){
            queryWrapper.eq(YddTag::getTagname,tagname);
        }*/
        yddTagService.page(listPage,queryWrapper);
        return ResultData.ok("", PageResult.init(listPage.getCurrent(),page,limit,listPage.getRecords()));
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(long id){
        log.info("删除{}成功",id);
        yddTagService.removeById(id);
        return ResultData.ok("删除成功",id);
    }
    @GetMapping("/all")
    public ResultData all(){
        List<YddTag> list = yddTagService.list();
        return ResultData.ok("",list);
    }
}
